<?php namespace Summer\Hjd\Controllers;

use Backend\Classes\Controller;
use BackendMenu;
use Illuminate\Http\Response;
use Input;
use RainLab\User\Models\User as UserModel;

class UserTuijian extends Controller
{
    protected $treeTitle="%s[%s][%s]";

    public $requiredPermissions = [
        'hjd_tuijian'
    ];

    public function __construct()
    {
        parent::__construct();
        \BackendMenu::setContext('Winter.System', 'main-menu-item-hjd_enduser');
        \Summer\Hub\Classes\SideNavManager::setContext('Summer.Hjd', 'side-menu-item-hjd_tuijian');
    }
    public function index()
    {
        $search_text=Input::get('search_text');
        if($search_text){
            $user_info=UserModel::with(['leveluinfo'])->where('userphone',$search_text)->orWhere('username',$search_text)->first();
            if($user_info){
                $one_tree['id']=$user_info->id;
                $one_tree['title']=sprintf($this->treeTitle,$user_info->username,$user_info->userphone,$user_info->realname);
                $one_tree['parentId']=$user_info->tuid;
                $one_tree['children']=[];
                $node_count=UserModel::where('tuid',$user_info->id)->count();
                if($node_count>0){
                    $one_tree['last']=false;
                }else{
                    $one_tree['last']=true;
                }
                $dataTree[]=$one_tree;
            }else{
                $dataTree=[];
            }
        }else{
            $first_list=UserModel::with(['leveluinfo'])->where('tdeep',0)->get();
            if($first_list->isNotEmpty()){
                foreach ($first_list as $flv){
                    $one_tree['id']=$flv->id;
                    $one_tree['title']=sprintf($this->treeTitle,$flv->username,$flv->userphone,$flv->realname);
                    $one_tree['parentId']=$flv->tuid;
                    $one_tree['children']=[];
                    $node_count=UserModel::where('tuid',$flv->id)->count();
                    if($node_count>0){
                        $one_tree['last']=false;
                    }else{
                        $one_tree['last']=true;
                    }
                    $dataTree[]=$one_tree;
                }
            }
        }
        $this->vars['dataTree'] =json_encode($dataTree);
        $this->vars['search_text'] =$search_text;
        $this->pageTitle="推荐关系";
        $this->addCss('/plugins/summer/hub/assets/layui/css/layui.css');
        $this->addCss('/plugins/summer/hub/assets/layui_ext/dtree/dtree.css');
        $this->addCss('/plugins/summer/hub/assets/layui_ext/dtree/font/dtreefont.css');
        $this->addJs('/plugins/summer/hub/assets/layui/layui.js');
    }

    public function getChildren(){
        $return['code']=0;
        $return['msg']='操作成功';
        $nodeId=Input::get('nodeId');
        if(!$nodeId){
            return false;
            $return['code']=-1;
            $return['msg']='操作失败';
            return response()->json(
                $return
            );
        }
        $child_list=UserModel::with(['leveluinfo'])->where('tuid',$nodeId)->get();
        if($child_list->isNotEmpty()){
            foreach ($child_list as $flv){
                $one_tree['id']=$flv->id;
                $one_tree['title']=sprintf($this->treeTitle,$flv->username,$flv->userphone,$flv->realname);
                $one_tree['parentId']=$flv->tuid;
                $one_tree['children']=[];
                $node_count=UserModel::where('tuid',$flv->id)->count();
                if($node_count>0){
                    $one_tree['last']=false;
                }else{
                    $one_tree['last']=true;
                }
                $dataTree[]=$one_tree;
            }
        }
        $return['data']=$dataTree;
        return response()->json(
            $return
        );
    }

}
